package com.ohos.carbon.provider;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.RecyclerDataBean;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class RecyclerViewProvider extends RecyclerAdapter<RecyclerViewProvider.ViewHolder>{

    private List<RecyclerDataBean> mLists;
    private Context context;

    public RecyclerViewProvider(List<RecyclerDataBean> mLists, Context context) {
        this.mLists = mLists;
        this.context = context;
    }

    @Override
    public RecyclerAdapter<ViewHolder>.ViewHolder onCreateViewHolder(ComponentContainer componentContainer) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_recycler_item, null, false);
        return new ViewHolder(component);
    }

    @Override
    public int getItemCount() {
        return mLists.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewHolder viewHolder = holder;
        if (mLists != null){
            viewHolder.icon.setImageAndDecodeBounds(mLists.get(position).getIcon());
            viewHolder.image.setImageAndDecodeBounds(mLists.get(position).getImage());
            viewHolder.mcontent.setText(mLists.get(position).getText());
        }

    }

    @Override
    public Object getItem(int i) {
        if (mLists != null && i >= 0 && i < mLists.size()) {
            return mLists.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    class ViewHolder extends RecyclerAdapter.ViewHolder {
        private Image icon, image;
        private Text mcontent;
        public ViewHolder(Component itemComponent) {
            super(itemComponent);
            icon = (Image) itemComponent.findComponentById(ResourceTable.Id_icon_recyc);
            image = (Image) itemComponent.findComponentById(ResourceTable.Id_image_recyc);
            mcontent = (Text) itemComponent.findComponentById(ResourceTable.Id_recycler_text);
        }
    }
}
